package com.ruoyi.rocky.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.rocky.domain.PcSysUser;
import com.ruoyi.rocky.domain.PcSysUserLogin;
import com.ruoyi.rocky.mapper.PcSysUserMapper;

import com.ruoyi.rocky.service.PcSysUserService;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
@Slf4j
public class PcSysUserServiceImpl implements PcSysUserService {

    @Autowired
    private PcSysUserMapper sysUserMapper;


    private PasswordEncoder passwordEncoder;

    public PcSysUserServiceImpl() {
    }

    /**
     * 添加用户
     *
     * @param sysUser
     * @return
     */
    @Override
    public R addSysUser(PcSysUser sysUser) {
        PcSysUser user1 = sysUserMapper.selByUserId(sysUser.getUserId());
        if (user1 != null) {
            return R.fail("用户已存在");
        }
        PcSysUser user = new PcSysUser();
        user.setAccount(sysUser.getAccount());
        user.setPassword(sysUser.getPassword());
        user.setPhone(sysUser.getPhone());
        user.setStatus(1);
        user.setCreateTime(DateUtils.getNowDate());
        sysUserMapper.addSysUser(user);
        return R.ok(true);
    }

    /**
     * 登录
     *
     * @param userLogin
     * @return
     */
    @Override
    public R login(PcSysUserLogin userLogin) {
        PcSysUser user = sysUserMapper.login(userLogin);
        if (null == user) {
            return R.fail("用户名或密码错误");
        }
//        if (user.getStatus() == 0) {
//            return R.fail("您已被禁用");
//        }
        //密码校验
        if ((user.getAccount().equals(userLogin.getAccount())) && (userLogin.getPassword().equals(user.getPassword()))) {
            System.out.println("登录成功");
            return R.fail("登录成功");
        } else {
            return R.fail("用户名或密码错误");
        }
    }

    /**
     * 修改账户密码
     *
     * @param userLogin
     * @return
     */
    @Override
    public R updateUserById(PcSysUserLogin userLogin) {
        PcSysUser user = new PcSysUser();
        user.setPassword(userLogin.getPassword());
        user.setUpdateTime(DateUtils.getNowDate());
        sysUserMapper.updateUserById(userLogin);
        return R.ok("修改成功");
    }

    /**
     * 查询所有账号
     *
     * @param
     * @return
     */
    @Override
    public List<PcSysUser> selAllUser() {
        List<PcSysUser> sysUsers = sysUserMapper.selAllUser();
        return sysUsers;
    }


}
